package version1;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class Mysql {
    private static String url;
    private static String username;
    private static String password;
    //2.静态方法：getParam（String 路径）--链接数据库--读取信息
    public static void getParam(String filename) throws IOException {
     //2.2
        Properties p=new Properties();
        //2.3
        InputStream fis=
      Mysql.class.getClassLoader().getResourceAsStream(filename);
        p.load(fis);

        //
        url= p.getProperty("url");
        username= p.getProperty("username");
        password= p.getProperty("password");
        System.out.println(":"+url+username+password);
    }

    public static void main(String[] args) throws IOException, SQLException {
        //测试封装概念--链接成功数据库
        Mysql.getParam("db_mysql.properties");
        //参考Mysql封装，写Oracle封装
        //链接成功Mysql-book表-增加一条记录操作
        //1.创建链接--Connection 代表Java链接成功数据库
        Connection con= DriverManager.getConnection(url,username,password);
        //2.执行sql--Statement 代表Java执行数据 sql
        Statement st=con.createStatement();
        //3.【CRUD操作：增删改 executeUpdate() 查 executeQuery】

        String sql="select * from book";
        ResultSet rs=st.executeQuery(sql);
        //System.out.println("成功添加了："+rows+"一条记录");
        while(rs.next()) {//next()获取当前表中所有的字段，有字段返回true

            int id = rs.getInt("id");
            String author = rs.getString("author");
            String name = rs.getString("name");
            int price = rs.getInt("price");
            String date = rs.getString("date");
            int number = rs.getInt("number");
            String discount = rs.getString("discount");
            System.out.println("序号：" + id + "作者：" + author + "书名：" + name + "价格：" + price + "日期：" + date + "数量" + number + "折扣情况" + discount);
            rs.close();
            st.close();
            con.close();

        }
    }
}
